package com.ruoyi.app.controller;

import com.ruoyi.app.domain.Card;
import com.ruoyi.app.domain.Patient;
import com.ruoyi.app.domain.Relationship;
import com.ruoyi.app.service.CardService;
import com.ruoyi.app.service.PatientService;
import com.ruoyi.app.service.RelationshipService;
import com.ruoyi.app.util.Result;
import com.ruoyi.common.redis.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("patient")
@RestController
public class PatientController {
    @Autowired
    private PatientService patientService;
    @Autowired
    private CardService cardService;
    @Autowired
    private RelationshipService relationshipService;
    @Autowired
    RedisService redisService;

    /**
     * 获取患者列表
     * @param userId
     * @return
     */
    @RequestMapping("list")
    public Result list(Integer userId)
    {
//        Integer id = redisService.getCacheObject("用户Id");
        Boolean aBoolean = redisService.hasKey("patient" + userId);
        if(aBoolean == true){
            return Result.ok(redisService.getCacheList("patient"+userId));
        }
        List<Patient> list = patientService.getList(userId);
        for (Patient patient : list) {
            Relationship relationship = relationshipService.getById(patient.getRelation());
            patient.setRelationName(relationship.getTypeName());
        }
        redisService.setCacheList("patient"+userId,list);
        return Result.ok(list);
    }
    /**
     * 添加患者
     */
    @RequestMapping("add")
    public Result add(@RequestBody Patient patient){
//        Integer id = redisService.getCacheObject("用户Id");
//        patient.setUserId(id);
        int i = patientService.add(patient);
        return Result.ok(i);
    }
    /**
     * 修改患者
     */
    @RequestMapping("updateByid")
    public Result updateByid(Integer id){
        Patient patient = patientService.getById(id);
        return Result.ok(patient);
    }
    @RequestMapping("update")
    public Result update(@RequestBody Patient patient){
        int i = patientService.update(patient);
        return Result.ok(i);
    }

    /**
     * 下拉菜单查看关系
     */
    @RequestMapping("getRelationship")
    public Result getRelationship(){
        List<Relationship> list = relationshipService.getRelationshipList();
        return Result.ok(list);
    }
    /**
     * 下拉菜单查看证件
     */
    @RequestMapping("getIdType")
    public Result getIdType(){
        List<Card> list = cardService.getCardList();
        return Result.ok(list);
    }

    /**
     * 根据id查找患者
     */
    @RequestMapping("getByNameById")
    public Result getByNameById(Integer patientId){
        Patient patient = patientService.getByNameById(patientId);
        patient.setRelationName(relationshipService.getById(patient.getRelation()).getTypeName());
        return Result.ok(patient);
    }

    /**
     * 体验服务本人
     */
    @RequestMapping("getPatinetSelect")
    public Result getPatinetSelect(Long userId){
        Patient patient = patientService.getPatinetSelect(userId);
        return Result.ok(patient);
    }
}
